/* Copyright (C) 2011 Riccardo Marson

   This file is part of OpenPool, an open-source game of pool. 

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   RICCARDO MARSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
   AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  See Readme.rtf for further information.
  
*/

#include "Constants.h"

#include "globjects.h"
#include <iostream>

void CPointLight::SetName(string &name)
{
	mName = name;
}

void CPointLight::SetAmbientColor(float red, float green , float blue, float alpha)
{
	mAmbientColor[0] = red;
	mAmbientColor[1] = green;
	mAmbientColor[2] = blue;
	mAmbientColor[3] = alpha;
}
void CPointLight::SetDiffuseColor(float red, float green , float blue, float alpha)
{
	mDiffuseColor[0] = red;
	mDiffuseColor[1] = green;
	mDiffuseColor[2] = blue;
	mDiffuseColor[3] = alpha;
}

void CPointLight::SetSpecularColor(float red, float green , float blue, float alpha)
{
	mSpecularColor[0] = red;
	mSpecularColor[1] = green;
	mSpecularColor[2] = blue;
	mSpecularColor[3] = alpha;
}

void CPointLight::SetPosition(CVector3 pos)
{
	mPosition[0] = pos.x();
	mPosition[1] = pos.y();
	mPosition[2] = pos.z();
	mPosition[3] = 1.0; // <--- positional light
}

string CPointLight::GetName()
{
	return mName;
}

float* CPointLight::GetPosition()
{
	return mPosition;
}

float* CPointLight::GetAmbientColor()
{
	return mAmbientColor;
}

float* CPointLight::GetDiffuseColor()
{
	return mDiffuseColor;
}

float* CPointLight::GetSpecularColor()
{
	return mSpecularColor;
}

CBasicCamera::CBasicCamera()
{
    mClipDistanceNear = 0.1;
    mClipDistanceFar = 500.0;
    mUp.SetComponents(0.0, 1.0, 0.0);
}

CBasicCamera::~CBasicCamera()
{
}

void CBasicCamera::SetName(string& name)
{
	mName = name;
}

void CBasicCamera::SetPosition(CVector3 position)
{
    mPosition = position;
}

void CBasicCamera::SetLookAt(CVector3 direction)
{
    mDirection = direction;
}

void CBasicCamera::SetUpVector(CVector3 up)
{
    mUp = up;
}

void CBasicCamera::Pitch(float angle)
{
    CVector3 relLookPoint( mDirection - mPosition );

    CVector3 relLookPoint2;
    float rad_angle = angle*mathc::DEG2RAD;

    relLookPoint2.SetComponents( relLookPoint.x(),
                                 relLookPoint.y()*cos(rad_angle) - relLookPoint.z()*sin(rad_angle),
                                 relLookPoint.z() );

    mDirection = relLookPoint2 + mPosition;
}

//Needs checking
void CBasicCamera::Yaw(float angle)
{
    CVector3 relLookPoint( mDirection - mPosition );

    CVector3 relLookPoint2;
    float rad_angle = angle*mathc::DEG2RAD;

    relLookPoint2.SetComponents( relLookPoint.x()*cos(rad_angle) - relLookPoint.z()*sin(rad_angle),
                                 relLookPoint.y(),
                                 relLookPoint.x()*sin(rad_angle) + relLookPoint.z()*cos(rad_angle));

    mDirection = relLookPoint2 + mPosition;
}

void CBasicCamera::Roll(float angle)
{
    //TODO
}

void CBasicCamera::SetClipDistanceNear(float nearPlane)
{
    mClipDistanceNear = nearPlane;
}

void CBasicCamera::SetClipDistanceFar(float farPlane)
{
    mClipDistanceFar = farPlane;
}

string CBasicCamera::GetName()
{
	return mName;
}

CVector3 CBasicCamera::GetPosition()
{
    return mPosition;
}

CVector3 CBasicCamera::GetLookAt()
{
    return mDirection;
}

CVector3 CBasicCamera::GetUpVector()
{
    return mUp;
}

float CBasicCamera::GetClipDistanceNear()
{
    return mClipDistanceNear;
}

float CBasicCamera::GetClipDistanceFar()
{
    return mClipDistanceFar;
}

COGLObject::COGLObject()
{
	mType = TYPE_STATIC;
    mId = OBJ_GENERIC;
    mModel = NULL;
    mHasModel = false;
    mRotationAngle = 0;
    mRotationAxis.SetComponents(1.0, 0.0, 0.0); //set a default rotation axis
    mHeading.SetComponents(0.0, 0.0, 1.0); //set a defualt heading
}

COGLObject::~COGLObject()
{
	mModel = NULL;
}

eObjectType & COGLObject::Type()
{
    return mType;
}

eObjectID & COGLObject::Id()
{
    return mId;
}


string & COGLObject::Name()
{
    return mName;
}

string & COGLObject::ModelFileName()
{
	return mModelFileName;
}

string & COGLObject::ColorMapName()
{
    return mColorMapName;
}

CVector3 & COGLObject::Position()
{
    return mPosition;
}

CVector3 & COGLObject::Heading()
{
    return mHeading;
}

CVector3 & COGLObject::RotationAxis()
{
    return mRotationAxis;
}

float & COGLObject::RotationAngle()
{
    return mRotationAngle;
}

float & COGLObject::Offset()
{
    return mOffset;
}

void COGLObject::SetModelPtr(const ModelOBJ* modelref)
{
	mModel = modelref;
}

const ModelOBJ* COGLObject::ModelPtr()
{
    return mModel;
}

bool & COGLObject::HasModel()
{
    return mHasModel;
}


bool & COGLObject::IsVisible()
{
    return mIsVisible;
}

CMaterial & COGLObject::Material()
{
    return mMaterial;
}
